<?php

/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$currentDir = dirname(__FILE__);
require_once($currentDir . "/sensapilityservice.php");
require_once($currentDir . "/utilities.php");

/**
 * @author api.jmatt@gmail.com
 * @version 0.1
 */

class SensapilitySiteFilter extends SensapilityService {
  
  function SensapilitySiteFilter($soapClient, 
                                 $namespaces, 
                                 $defaultNamespaceLabel) {
    parent::SensapilityService($soapClient, 
                               $namespaces, 
                               $defaultNamespaceLabel);
  }

  /**
   * Add a filter
   * @param string $synServiceId the id corresponding to the service to
   * add the filter for
   * @param string[] $filteredUrls the url(s) to filter ads from
   * @return mixed the empty string if the cal succeeds, null if there is an
   * error
   */
  function addSiteFilters($synServiceId, $filteredUrls) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':synServiceId' => $synServiceId);
    $callParameters = ArrayToXml($callParameters);
    $keys = array_keys($filteredUrls);
    foreach ( $keys as $key ) {
      $tempArray = array("siteFilters" => $filteredUrls[$key]);
      $callParameters .= ArrayToXml($tempArray);
    }

    $callResults = 
      $this->__soapClient->call('addSiteFilters',
                                $callParameters,
                                $this->__soapClient->namespaces[$ns]);
    
    $faultHappened = parent::checkForFailure($callResults);
    if ( $faultHappened ) {
      echo $faultHappened;
      $returnValue = null;
    } else {
      $returnValue = $callResults;
    }
    return $returnValue;
  }

  /**
   * Set the list of site filters, will erase the existing set of filters
   * @param string $synServiceId the id corresponding to the service to
   * add the filter for
   * @param string[] $filteredUrls the url(s) to filter ads from
   * @return mixed the empty string if the cal succeeds, null if there is an
   * error
   */
  function setSiteFilters($synServiceId, $filteredUrls) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':synServiceId' => $synServiceId);
    $callParameters = ArrayToXml($callParameters);
    $keys = array_keys($filteredUrls);
    foreach ( $keys as $key ) {
      $tempArray = array("siteFilters" => $filteredUrls[$key]);
      $callParameters .= ArrayToXml($tempArray);
    }

    $callResults = 
      $this->__soapClient->call('setSiteFilters',
                                $callParameters,
                                $this->__soapClient->namespaces[$ns]);
    
    $faultHappened = parent::checkForFailure($callResults);
    if ( $faultHappened ) {
      echo $faultHappened;
      $returnValue = null;
    } else {
      $returnValue = $callResults;
    }
    return $returnValue;
  }

  /**
   * Get the set of site filters
   * @param string $synServiceId the id corresponding to the account to get the
   * list of filters for
   * @param string[] list of urls filtered for this account
   */
  function getSiteFilters($synServiceId) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':synServiceId' => $synServiceId);

    $callResults = 
      $this->__soapClient->call('getSiteFilters',
                                ArrayToXml($callParameters),
                                $this->__soapClient->namespaces[$ns]);
    
    $faultHappened = parent::checkForFailure($callResults);
    if ( $faultHappened ) {
      echo $faultHappened;
      $returnValue = null;
    } else {
      // if there is a single result, wrap it in an array so there is a
      // consistent return type. If there is no result, return empty string
      if ( !is_array($callResults) && "" != $callResults) {
        $callResults = array($callResults);
      }
      $returnValue = $callResults;
    }
    return $returnValue;
  }

  /**
   * Remove site filters
   * @param string $synServiceId the id corresponding to the service to
   * add the filter for
   * @param string[] $filteredUrls the url(s) to remove from the filter set
   * @return mixed the empty string if the call succeeds, null otherwise
   */
  function removeSiteFilters($synServiceId, $filteredUrls) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':synServiceId' => $synServiceId);
    $callParameters = ArrayToXml($callParameters);
    $keys = array_keys($filteredUrls);
    foreach ( $keys as $key ) {
      $tempArray = array($ns . ':siteFilters' => $filteredUrls[$key]);
      $callParameters .= ArrayToXml($tempArray);
    }

    $callResults = 
      $this->__soapClient->call('removeSiteFilters',
                                $callParameters,
                                $this->__soapClient->namespaces[$ns]);
    
    $faultHappened = parent::checkForFailure($callResults);
    if ( $faultHappened ) {
      echo $faultHappened;
      $returnValue = null;
    } else {
      $returnValue = $callResults;
    }
    return $returnValue;
  }
}
?>
